/**
 * 简化Phase 2控制器 - 用于调试
 * EndoSight-UC 医疗AI系统
 */

import auditService from '../services/auditService.js';

/**
 * 获取Phase 2版本信息
 */
export const getVersion = async (req, res) => {
  try {
    await auditService.logMedicalDataAccess({
      userId: req.user?.id || 'anonymous',
      sessionId: req.sessionId,
      patientId: null,
      dataType: 'api_access',
      accessType: 'view',
      accessReason: 'Phase 2版本信息查询',
      dataBefore: null,
      dataAfter: { version: '2.0.0', phase: 'Phase 2 - Multi-Process Architecture' },
      phiPresent: false,
      consentVerified: false,
      accessGranted: true
    });

    res.json({
      success: true,
      message: 'Phase 2 版本信息获取成功',
      data: {
        version: '2.0.0',
        phase: 'Phase 2 - Multi-Process Architecture',
        description: 'EndoSight-UC 医疗AI系统多进程架构',
        features: [
          'Multi-process AI inference',
          'Intelligent load balancing',
          'Priority task queuing',
          'Real-time monitoring',
          'Automatic failure recovery',
          'HIPAA compliant audit logging'
        ],
        status: '正在调试',
        timestamp: new Date().toISOString()
      }
    });
  } catch (error) {
    console.error('Phase 2 版本信息获取失败:', error);
    res.status(500).json({
      success: false,
      message: 'Phase 2 版本信息获取失败',
      error: error.message
    });
  }
};

/**
 * 获取简化进程状态
 */
export const getProcessStatus = async (req, res) => {
  try {
    await auditService.logMedicalDataAccess({
      userId: req.user?.id || 'anonymous',
      sessionId: req.sessionId,
      patientId: null,
      dataType: 'system_status',
      accessType: 'view',
      accessReason: 'Phase 2进程管理器状态查询',
      dataBefore: null,
      dataAfter: { status: 'simplified_mode', message: '简化模式运行中' },
      phiPresent: false,
      consentVerified: false,
      accessGranted: true
    });

    res.json({
      success: true,
      message: 'Phase 2 进程状态获取成功 (简化模式)',
      data: {
        status: 'simplified_mode',
        message: 'Phase 2 控制器正常运行，完整功能正在调试中',
        processManager: {
          status: 'active',
          workers: {
            total: 4,
            active: 2,
            types: {
              ai_inference: 1,
              file_processing: 1,
              cleanup: 1,
              monitoring: 1
            }
          }
        },
        timestamp: new Date().toISOString()
      }
    });
  } catch (error) {
    console.error('Phase 2 进程状态获取失败:', error);
    res.status(500).json({
      success: false,
      message: 'Phase 2 进程状态获取失败',
      error: error.message
    });
  }
};

/**
 * 健康检查
 */
export const healthCheck = async (req, res) => {
  try {
    res.json({
      success: true,
      message: 'Phase 2 健康检查通过',
      data: {
        status: 'healthy',
        phase: 'Phase 2 - Multi-Process Architecture',
        controller: 'simplified',
        timestamp: new Date().toISOString()
      }
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: 'Phase 2 健康检查失败',
      error: error.message
    });
  }
};